####################################
# SCons build system for Pedigree
## Tyler Kennedy (AKA Linuxhq AKA TkTech)
####################################

import os.path
Import(['env'])

# Temporary copy of the global environment so we can fiddle with flags
tmpEnvironment = env.Clone()

# Change to then name of this module
kerneldir = "#" + env["PEDIGREE_BUILD_KERNEL"]
imagesdir = env["PEDIGREE_IMAGES_DIR"]
builddir = env["PEDIGREE_BUILD_BASE"]
objname = kerneldir + '/kernel'

env.Alias("kernel", objname)

# To include a new subdirectory just add to the list.
subdirs = [

]

# To include new files just add to the list, or Glob("*.cpp")
files = [
    'Log.cc',
    '#' + builddir + '/Version.cc',
    'Archive.cc',
    'Spinlock.cc',
    'Subsystem.cc',
    'ServiceManager.cc',
    'Service.cc',
    Glob('utilities/*.cc'),
    Glob('utilities/sha1/*.cc'),
    Glob('utilities/md5/*.cc'),
    Glob('machine/*.cc'),
    Glob('linker/*.cc'),
    Glob('core/*.cc'),
    Glob('core/process/*.cc'),
    Glob('core/processor/*.cc'),
    Glob('core/lib/*.cc'),
    Glob('core/lib/*.c'),
    Glob('config/*.cc'),
    Glob('network/*.cc'),
    Glob('graphics/*.cc'),
]

if 'STATIC_DRIVERS' in env['CPPDEFINES']:
    files += env['INITRD_LIST']

if 'DEBUGGER' in env['CPPDEFINES']:
    files += [
        Glob('debugger/*.cc'),
        Glob('debugger/commands/*.cc')
    ]
else:
	files += [
		Glob('debugger/panic.cc'),
		Glob('debugger/SerialIO.cc'),
		Glob('debugger/LocalIO.cc'),
		Glob('debugger/DebuggerIO.cc'),
	]

libudis86_files = [
]

# To include a new directory for includes, just add it to the list
include = [
    '#/src/system/include',
    '#/src/modules/system',
    '#/src/modules',
    'debugger/',
    'debugger/commands/',
    'debugger/libudis86/',
    'linker/',
    'core/',
    'core/lib/',
    '.'
]

# To add a library add to the list below
libraries = [
    'gcc'
]

if env['ARCH_TARGET'] in ['X86', 'X64', 'ARM']:
    libraries += ['kernelrtti']

# To add a library path add to the list below
libpaths = [
    '#',
    imagesdir + 'libraries',
    # env['LIBGCC']
]

#####################################
## Architecture-specific files
#####################################
if 'X86_COMMON' in env['CPPDEFINES']:
    files += [
        Glob('machine/x86_common/*.cc'),
        'machine/x86_common/x86emu/debug.c',
        'machine/x86_common/x86emu/decode.c',
        'machine/x86_common/x86emu/fpu.c',
        'machine/x86_common/x86emu/ops.c',
        'machine/x86_common/x86emu/ops2.c',
        'machine/x86_common/x86emu/prim_ops.c',
        'machine/x86_common/x86emu/sys.c',
    ]

    commonFiles = Glob('core/processor/x86_common/*.cc')

    if 'DEBUGGER' in env['CPPDEFINES']:
        files += [Glob('debugger/libudis86/*.c')]
        files += commonFiles
    else:
		files += filter(lambda x: x.path not in [env.File('core/processor/x86_common/Disassembler.cc').path], commonFiles)
    include += ['machine/x86_common/x86_emu/x86emu/']

if 'ARM_VERSATILE' in env['CPPDEFINES']:
    files += [
        Glob('machine/arm_versatile/*.cc'),
        ]
    include += ['machine/arm_versatile']

if env['arm_9']:
    files += [Glob('core/processor/arm_926e/*.cc')]
    files += [Glob('core/processor/arm_926e/asm/*.S')]
elif env['armv7']:
    files += [Glob('core/processor/armv7/*.cc')]
    files += [Glob('core/processor/armv7/asm/*.S')]

if env['ARCH_TARGET'] == 'ARM':
    files += ['boot-arm.cc']
    files += [Glob('core/processor/arm_common/*.cc')]

if env['arm_beagle']:
    files += [Glob('machine/arm_beagle/*.cc')]
    files += [Glob('machine/arm_beagle/*.c')]

if env['ARCH_TARGET'] == 'X86':
    files += [
        Glob('core/processor/x86/*.cc'),
        'core/processor/x86/asm/boot-standalone.s',
        'core/processor/x86/asm/InterruptManager.s',
        'core/processor/x86/asm/Processor.s',
        'core/processor/x86/asm/Scheduler.s',
        # 'core/processor/x86/asm/multiprocessor.s'
    ]

    if 'MULTIPROCESSOR' in env['CPPDEFINES']:
        files += ['core/processor/x86/asm/trampoline.s']

if env['ARCH_TARGET'] == 'X64':
    files += [
        Glob('core/processor/x64/*.cc'),
        'core/processor/x64/asm/boot-standalone.s',
        'core/processor/x64/asm/InterruptManager.s',
        'core/processor/x64/asm/SyscallManager.s',
        'core/processor/x64/asm/Processor.s',
        'core/processor/x64/asm/Scheduler.s',
        'core/processor/x64/asm/gdt.s'
    ]

tmpEnvironment['CPPPATH'] = include
tmpEnvironment['LIBS'] = libraries
tmpEnvironment['LIBPATH'] = libpaths

SConscript([os.path.join(i, 'SConscript') for i in subdirs],exports = ['env'])

if env['ARCH_TARGET'] == 'X64':
    tmpEnvironment['LINKFLAGS'] += ' -m64 -mcmodel=kernel'
tmpEnvironment.Program(objname, files)

